package com.ruoyi.guest.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.address.domain.Address;
import com.ruoyi.guest.mapper.GuestMapper;
import com.ruoyi.guest.domain.Guest;
import com.ruoyi.guest.service.IGuestService;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;
import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 顾客信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-31
 */
@Service
public class GuestServiceImpl implements IGuestService 
{
    @Autowired
    private GuestMapper guestMapper;

    /**
     * 查询顾客信息
     * 
     * @param guestId 顾客信息主键
     * @return 顾客信息
     */
    @Override
    public Guest selectGuestByGuestId(Long guestId)
    {
        return guestMapper.selectGuestByGuestId(guestId);
    }

    /**
     * 根据微信openId查询顾客信息
     *
     * @param wechatOpenid 微信唯一标识id
     * @return 顾客信息
     */
    @Override
    public Guest selectGuestByWechatOpenId(String wechatOpenid)
    {
        return guestMapper.selectGuestByWechatOpenId(wechatOpenid);
    }

    /**
     * 查询顾客信息列表
     * 
     * @param guest 顾客信息
     * @return 顾客信息
     */
    @Override
    public List<Guest> selectGuestList(Guest guest)
    {
        return guestMapper.selectGuestList(guest);
    }

    /**
     * 新增顾客信息
     * 
     * @param guest 顾客信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertGuest(Guest guest)
    {
        // 密码加密 若依自带
        guest.setPassword(SecurityUtils.encryptPassword(guest.getPassword()));
        // 创建时间
        guest.setCreateTime(DateUtils.getNowDate());
        // 创建人
        guest.setCreateBy(getUsername());
        // 创建人id
        guest.setUserId(getUserId());
        // guest.setDeptId(getDeptId());
        int rows = guestMapper.insertGuest(guest);
        insertAddress(guest);
        return rows;
    }

    /**
     * 修改顾客信息
     * 
     * @param guest 顾客信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateGuest(Guest guest)
    {
        guest.setUpdateTime(DateUtils.getNowDate());
        guestMapper.deleteAddressByGuestId(guest.getGuestId());
        insertAddress(guest);
        return guestMapper.updateGuest(guest);
    }

    /**
     * 批量删除顾客信息
     * 
     * @param guestIds 需要删除的顾客信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteGuestByGuestIds(Long[] guestIds)
    {
        guestMapper.deleteAddressByGuestIds(guestIds);
        return guestMapper.deleteGuestByGuestIds(guestIds);
    }

    /**
     * 删除顾客信息信息
     * 
     * @param guestId 顾客信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteGuestByGuestId(Long guestId)
    {
        guestMapper.deleteAddressByGuestId(guestId);
        return guestMapper.deleteGuestByGuestId(guestId);
    }

    /**
     * 根据用户id查询顾客信息
     *
     * @param userId 用户id
     * @return 顾客信息
     */
    @Override
    public Guest selectGuestByUserId(Long userId) {
        // 示例：假设员工表有sys_user_id字段关联系统用户
        return guestMapper.selectGuestByUserId(userId);
    }

    /**
     * 新增收货地址信息信息
     * 
     * @param guest 顾客信息对象
     */
    public void insertAddress(Guest guest)
    {
        List<Address> addressList = guest.getAddressList();
        Long guestId = guest.getGuestId();
        if (StringUtils.isNotNull(addressList))
        {
            List<Address> list = new ArrayList<Address>();
            for (Address address : addressList)
            {
                address.setGuestId(guestId);
                list.add(address);
            }
            if (list.size() > 0)
            {
                guestMapper.batchAddress(list);
            }
        }
    }
}
